package com.lixinyue.p2.service;

import com.lixinyue.p2.bean.Customer;

public class CustomerList {

    private Customer[] customers;
    private int total = 0;//记录已保存客户对象的个数


    /**
     * 用来初始化数组
     *
     * @param totalCustomer 指定customers数组最大空间
     */
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    /**
     * @param customer 指定要添加的对象
     * @return 添加成功返回true，false表示数组已满
     */
    public boolean addCustomer(Customer customer) {
        if (total >= customers.length) {
            return false;
        }
        customers[total++] = customer;
        return true;
    }

    /**
     * @param index
     * @param cust
     * @return
     */
    public boolean repalceCustomer(int index, Customer cust) {
        if (index >= total || index < 0) {
            return false;
        }
        customers[index] = cust;
        return true;
    }

    public boolean deleteCustomer(int index)
    {
        if (index >= total || index < 0) {
            return false;
        }
        for (int i = index; i < total - 1; i++) {
            customers[i] = customers[i + 1];
        }
        customers[--total] = null;
        return true;

    }
    public  Customer[] getAllCustomer()
    {
        Customer[] cust=new Customer[total];
        for (int i = 0; i < total; i++) {
            cust[i]=customers[i];
        }
        return  cust;
    }
    public Customer getCutomer(int index)
    {
        if(index<0||index>=total)
        {
            return null;
        }
        return customers[index];
    }
    public  int getTotal()
    {
        return total;
    }




}
